package com.jh.paymentchannel.helipay.service;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.jh.paymentchannel.util.Util;
import com.jh.paymentchannel.util.jifu.DateTime;

import cn.jh.common.utils.UUIDGenerator;
import net.sf.json.JSONObject;
@Controller
@EnableAutoConfiguration
public class BangKaService{
private static final Logger log = LoggerFactory.getLogger(BangKaService.class);
	
		@Autowired
		Util util;
		private String merchant_no = "C1800001614";

		@RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/pay/bk")
		public @ResponseBody Object queryBalance(HttpServletRequest request,
				@RequestParam(value = "userId") long userId,
				@RequestParam(value = "loginPhone") String loginPhone,
				@RequestParam(value = "cardNo") String cardNo
				) {
		Map map = new HashMap();
		RestTemplate restTemplate=new RestTemplate();
		URI uri = util.getServiceUrl("user", "error url request!");
		String url = uri.toString() + "/v1.0/user/bank/find/bankphone";
		MultiValueMap<String, String> requestEntity  = new LinkedMultiValueMap<String, String>();
		requestEntity.add("userId", String.valueOf(userId));
		requestEntity.add("cardNo", cardNo);
		String result = restTemplate.postForObject(url, requestEntity, String.class);
		log.info("接口/v1.0/user/bank/find/bankphone--RESULT================"+result);
		JSONObject jsonObject =  JSONObject.fromObject(result);
		JSONObject resultObj  =  jsonObject.getJSONObject("result");
		String userName =  resultObj.getString("userName");
		String bankName = resultObj.getString("bankName");
		String securityCode = "";
		String month = "";
		String year = "";
		String expiredTime = "";
		if(resultObj.containsKey("nature")&&resultObj.getString("nature").contains("贷记")) {
			  expiredTime = resultObj.getString("expiredTime");//付款信用卡有效期,格式YYMM
			  year = expiredTime.substring(0, 2);
			  month = expiredTime.substring(expiredTime.length()-2, expiredTime.length());
			  securityCode = resultObj.getString("securityCode");//付款信用卡安全码
		}
		String phone = resultObj.getString("phone"); //预留信用卡手机号码
		String idcard = resultObj.getString("idcard");//身份证号
		String ordercode = UUIDGenerator.getUUID();
		String reqTime = DateTime.getNow("yyyyMMddHHmmss");
		String data;
		if("上海银行".equals(bankName.trim())) {
			map.put("resp_code", "999999");
			map.put("resp_message", "不支持上海银行");
			return map;
		}
		try {
			data = "?P1_bizType=QuickPayBindCard&P2_customerNumber="+merchant_no+"&P3_userId="+userId
					+"&P4_orderId="+ordercode+"&P5_timestamp="+reqTime+"&P6_payerName="
					+URLEncoder.encode(userName,"UTF-8")+"&P7_idCardType=IDCARD"
					+"&P8_idCardNo="+idcard+"&P9_cardNo="+cardNo+"&P10_year="+year+"&P11_month="+month+"&bankName="+URLEncoder.encode(bankName,"UTF-8")
					+"&P12_cvv2="+securityCode+"&P13_phone="+phone+"&loginPhone="+loginPhone;
			log.info("======================"+data+"======================");
			url = "http://47.104.158.3/v1.0/paymentchannel/topup/heli"+data;
			map.put("resp_code", "000000");
			map.put("result", url);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			log.info("======================"+e.getMessage()+"======================");
		}
		return map; 
	}
}
